import { Injectable, Inject } from '@nestjs/common';
import { Model ,Types} from 'mongoose';
import { User } from 'src/db/schemas/user.schema';
import { AddUser, EditUser , UserId } from './user.dto'

import { Photo } from 'src/db/schemas/photo.schema';
/**用户模块服务 */
@Injectable()
export class UserService {
	constructor(
		@Inject('USER_MODEL')
		private readonly UserModel: Model<User>,
		@Inject('PHOTO_MODEL')
		private readonly PhotoModel: Model<Photo>,
	) { }

	async login(name: string):Promise<User>{
		return await this.UserModel.findOne({name});
	}

	async findUser(): Promise<User[]> {
		return this.UserModel.find().exec();
	}

	async addUser(Dto: AddUser){
		var user=new this.UserModel(Dto)
		return user.save()
	}

	async updateUser(id:string,params){
		return this.UserModel.findByIdAndUpdate(id,params)
	}

	async deleteUser(id:string){
		return this.UserModel.deleteOne({_id:id})
	}

	async getUserPhotos(id:string){
		var _id=new Types.ObjectId(id) as unknown as string
		return this.PhotoModel.find({user:_id})//6172a0e54b3630fb83ae0e54
	}
}
